import requests
import pandas as pd
from lxml import etree
from pyecharts.charts import Page
from pyecharts.components import Table
from pyecharts.options import ComponentTitleOpts

import UserAgents


def monitor_data():
    url = "http://www.100ppi.com/monitor/"

    ua = UserAgents.ua_generator()

    response = requests.request('GET', url, headers={'User-agent': ua})
    content = response.text
    html = etree.HTML(content)

    table = html.xpath("//div[@class='right fl']//table//tr[@align='center']//td")

    datalist = []
    for line in table:
        txt = line.xpath(".//a/text() | "
                         "./text()")
        if len(txt) == 3:
            try:
                n = txt.pop(1)
            except Exception as error:
                print("Monitor Fetch Error: {}, Replaced with 0.".format(error))
                n = 0
            else:
                pass
        else:
            try:
                n = txt.pop()
            except Exception as error:
                print("Monitor Fetch Error: {}, Replaced with 0.".format(error))
                n = 0
            else:
                pass
        datalist.append(n)
    pis_num = int(len(datalist)/5)

    monitor_data = []
    for x in range(pis_num):
        m = x*5
        try:
            weekly_chg = round(float((float(datalist[m+2])-float(datalist[m+3]))/float(datalist[m+3])*100), 2)
        except ZeroDivisionError:
            weekly_chg = 0
        else:
            pass
        try:
            monthly_chg = round(float((float(datalist[m+2])-float(datalist[m+4]))/float(datalist[m+4])*100), 2)
        except ZeroDivisionError:
            monthly_chg = 0
        else:
            pass
        piece = [datalist[m], datalist[m+1], datalist[m+2], datalist[m+3], datalist[m+4], weekly_chg, monthly_chg]
        monitor_data.append(piece)

    header = ['商品名称', '型号', '最新报价', '上周初报价', '上月初报价', '相对上周初变化百分比', '相对上月初变化百分比']

    df = pd.DataFrame(monitor_data, columns=header)
    df.sort_values(by='相对上周初变化百分比', ascending=False, inplace=True)
    df.to_csv('..//spotdata//spot_monitor.csv', encoding='gbk', index=False)


def spot_monitor():
    df = pd.read_csv('..//spotdata//spot_monitor.csv', encoding='gbk')
    header = df.columns.to_list()
    package = []
    for i in range(len(df.index.to_list())):
        line = df.iloc[i].to_list()
        package.append(line)

    table = (
        Table()
        .add(header, package)
        .set_global_opts(title_opts=ComponentTitleOpts(title="每日商品现货价格监控"))
    )

    page = Page(page_title='价格监控页面', layout=Page.SimplePageLayout)
    page.add(
        table
    )
    page.render('价格监控页面.html')


